package com.hsq.androidproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.db.DBHelper;
import com.hsq.androidproject.entity.bd_entity.TopicsLike;
import com.hsq.androidproject.entity.find.FindComment;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by lenovo on 2016/11/22.
 */

public class MyListViewCommentAdapter extends BaseAdapter {        ////热门讨论区
    private List<FindComment.PostsBean> mData;
    private Context context;
    private DBHelper dbHelper;
    private String userName = "peng";

    public MyListViewCommentAdapter(List<FindComment.PostsBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_one_comment_listview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.iv_zan = (ImageView) convertView.findViewById(R.id.iv_zan);
            viewHolder.ll_layout = (LinearLayout) convertView.findViewById(R.id.ll_layout);
            viewHolder.tv_userNick = (TextView) convertView.findViewById(R.id.tv_userNick);
            viewHolder.tv_likeCount = (TextView) convertView.findViewById(R.id.tv_likeCount);
            viewHolder.tv_floor = (TextView) convertView.findViewById(R.id.tv_floor);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_postTime = (TextView) convertView.findViewById(R.id.tv_postTime);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final FindComment.PostsBean postsBean = mData.get(position);
        final String thumb = postsBean.getUserAvatar();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideCircleTransform(context))//圆形
                .into(viewHolder.iv_img);

        final String userNick = postsBean.getUserNick();
        viewHolder.tv_userNick.setText(userNick);

        int floor = postsBean.getFloor();
        viewHolder.tv_floor.setText(floor + "楼   ");

        viewHolder.tv_title.setText(postsBean.getContent());

        long postTime = postsBean.getPostTime();          ///////postTime 应该是:lastReplyTime
        String time = getTimeFromMillisecond(postTime);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());

        long diff = 0;
        long hours = 0;
        long day = 0;
        long minutes = 0;
        try {
            Date d1 = sdf.parse(date);
            Date d2 = sdf.parse(time);
            diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            day = days;
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (hours > 0) {///hours + ":" + minutes
            viewHolder.tv_postTime.setText(hours + "小时前");
        } else if (hours == 0) {
            if (minutes > 1) {
                viewHolder.tv_postTime.setText(minutes + "分钟前");
            } else {
                viewHolder.tv_postTime.setText("刚刚");
            }
        } else if (hours < 0 && hours > -23) {
            viewHolder.tv_postTime.setText("昨天");
        } else {
            viewHolder.tv_postTime.setText(day + "天前");
        }
        dbHelper = new DBHelper(context);
        final int likeCount = postsBean.getLikeCount();
        final TopicsLike topicsLike = new TopicsLike(viewHolder.tv_title.getText().toString(), userName);
        if (dbHelper.isExistT(topicsLike)) {
            int count = likeCount + 1;
            viewHolder.tv_likeCount.setText(count + "");
            viewHolder.iv_zan.setImageResource(R.drawable.like_icon_highlight);
        } else {
            viewHolder.tv_likeCount.setText(likeCount + "");
            viewHolder.iv_zan.setImageResource(R.drawable.like_icon);
        }
        viewHolder.iv_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //////赞
                if (dbHelper.isExistT(topicsLike)) {
                    viewHolder.tv_likeCount.setText(likeCount + "");
                    viewHolder.iv_zan.setImageResource(R.drawable.like_icon);
                    dbHelper.deleteTopicsLikeByNameAndUserName(topicsLike);
                } else {
                    int count = likeCount + 1;
                    viewHolder.tv_likeCount.setText(count + "");
                    viewHolder.iv_zan.setImageResource(R.drawable.like_icon_highlight);
                    dbHelper.insertTopicsLike(topicsLike);
                }

            }
        });

        return convertView;
    }

    public static String getTimeFromMillisecond(Long millisecond) {		/*毫秒转换为时间*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(millisecond);
        String timeStr = simpleDateFormat.format(date);
        return timeStr;
    }


    class ViewHolder {
        public ImageView iv_img;        //头像
        private TextView tv_userNick, tv_likeCount, tv_floor, tv_postTime, tv_title; //// 姓名 赞  几楼 时间  标题
        private LinearLayout ll_layout;
        public ImageView iv_zan;        ////赞  ////like_icon_highlight src
    }


}
